package com.achievement.project.system.score.controller;

import java.util.*;

import com.achievement.common.utils.security.ShiroUtils;
import com.achievement.project.system.choose.domain.CourseChoose;
import com.achievement.project.system.choose.service.ICourseChooseService;
import com.achievement.project.system.role.domain.Role;
import com.achievement.project.system.role.service.IRoleService;
import com.achievement.project.system.teacher.domain.Teacher;
import com.achievement.project.system.teacher.service.ITeacherService;
import com.achievement.project.system.user.domain.User;
import com.achievement.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.achievement.framework.aspectj.lang.annotation.Log;
import com.achievement.framework.aspectj.lang.enums.BusinessType;
import com.achievement.project.system.score.domain.Score;
import com.achievement.project.system.score.service.IScoreService;
import com.achievement.framework.web.controller.BaseController;
import com.achievement.framework.web.domain.AjaxResult;
import com.achievement.common.utils.poi.ExcelUtil;
import com.achievement.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 成绩详情页面Controller
 *
 * @author zkai
 * @date 2021-04-24
 */
@Controller
@RequestMapping("/system/score")
public class ScoreController extends BaseController
{
    private String prefix = "system/score";

    @Autowired
    private IScoreService scoreService;

    @Autowired
    private ITeacherService teacherService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private ICourseChooseService courseChooseService;

    @RequiresPermissions("system:score:view")
    @GetMapping()
    public String score()
    {
        return prefix + "/score";
    }


    /**
     * 查询成绩详情页面列表
     */
    @RequiresPermissions("system:score:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Score score)
    {
        startPage();
        Long userId = ShiroUtils.getUserId();
        Set<String> strings = roleService.selectRoleKeys(userId);
        Iterator<String> it = strings.iterator();
        String roleKey = "";
        while (it.hasNext()){
            roleKey = it.next();
        }
        List<Score> list = null;
        if ("teacher".equals(roleKey)){
            Teacher teacher = teacherService.selectTeacherById(userId);
            if (!"admin".equals(ShiroUtils.getLoginName())){
                score.setTeacherId(userId);
            }
            score.setGradeId(String.valueOf(teacher.getGradeId()));
            list = scoreService.selectScoreList(score);

        }
        return getDataTable(list);
    }


    @Log(title = "导入成绩", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
        List<Score> scoreList = util.importExcel(file.getInputStream());
        String message = scoreService.importUser(scoreList, updateSupport);
        return AjaxResult.success(message);
    }

    @RequiresPermissions("system:score:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
        return util.importTemplateExcel("成绩数据");
    }

    /**
     * 导出成绩详情页面列表
     */
    @RequiresPermissions("system:score:export")
    @Log(title = "成绩详情页面", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Score score)
    {
        Long userId = ShiroUtils.getUserId();
        Set<String> strings = roleService.selectRoleKeys(userId);
        Iterator<String> it = strings.iterator();
        String roleKey = "";
        while (it.hasNext()){
            roleKey = it.next();
        }
        List<Score> list = null;
        if ("teacher".equals(roleKey)){
            Teacher teacher = teacherService.selectTeacherById(userId);
            if (!"admin".equals(ShiroUtils.getLoginName())){
                score.setTeacherId(userId);
            }
            score.setGradeId(String.valueOf(teacher.getGradeId()));
            list = scoreService.selectScoreList(score);

        }
        ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
        return util.exportExcel(list, "成绩详情页面数据");
    }

    /**
     * 新增成绩详情页面
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    /**
     * 新增保存成绩详情页面
     */
    @RequiresPermissions("system:score:add")
    @Log(title = "成绩详情页面", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Score score)
    {
        return toAjax(scoreService.insertScore(score));
    }

    /**
     * 修改成绩详情页面
     */
    @GetMapping("/setScore/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Score score = scoreService.selectScoreById(id);
        mmap.put("score", score);
        return prefix + "/edit";
    }

    /**
     * 修改保存成绩详情页面
     */
    @RequiresPermissions("system:score:edit")
    @Log(title = "成绩详情页面", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Score score)
    {
        return toAjax(scoreService.updateScore(score));
    }

    /**
     * 删除成绩详情页面
     */
    @RequiresPermissions("system:score:remove")
    @Log(title = "成绩详情页面", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(scoreService.deleteScoreByIds(ids));
    }


    /**
     * 折线图
     */
    @PostMapping("/getEchartsForPolyline")
    @ResponseBody
    public List<Map<String,Object>>  getEchartsForPolyline()
    {
        Long userId = ShiroUtils.getUserId();
        List<Map<String,Object>> echartsForPolyline = scoreService.getEchartsForPolyline(userId);
        return echartsForPolyline;
    }
}
